<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="pageParam.model.name"
        style="width: 200px;"
        placeholder="模糊查询..."
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @input="handleFilter"
      />
    </div>
    <!-- 表格 -->
    <el-table :data="list" fit highlight-current-row style="width: 100%;" @sort-change="sortChange">
      <el-table-column class="filter-item" type="expand">
        <template slot-scope="{row}">
          <div v-html="row.requestData"></div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="访问人姓名"/>
      <el-table-column sortable="custom" prop="time" label="访问时间"/>
      <el-table-column prop="uri" label="访问接口名"/>
      <el-table-column prop="ip" label="访问的ip"/>
      <el-table-column label="操作" fit>
        <template slot-scope="{row,$index}">
          <el-button size="small" type="danger" @click="handleDel(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getListByPage"
    />
  </div>
</template>

<script>
import {getAll, del} from '@/api/role'
// 导入组件  记得声明组件
import Pagination from '@/components/Pagination'

export default {
  components: { // 在这里声明组件
    Pagination
  },
  data() {
    return {
      list: [], // 列表数据
      total: 0,
      listQuery: { // 分页 排序  查询参数
        pageNum: 1,
        pageSize: 5
      },
      pageParam: {
        pageNum: 1,
        pageSize: 5,
        model: {
          name: ''
        },
        orderParams: []
      },
    }
  },
  created() {
    this.getListByPage()
  },
  methods: {
    sortChange(val){
      const order = val.order === 'ascending' ? 'asc' : 'desc'
       let orderParam = 'time' + ' ' + order
      this.pageParam.orderParams = []
      this.pageParam.orderParams.push(orderParam)
      this.getListByPage()
    },
    getListByPage() {
      this.pageParam.pageNum = this.listQuery.pageNum
      this.pageParam.pageSize = this.listQuery.pageSize
      getAll(this.pageParam).then(res => {
        // console.log(res)
        this.list = res.content.list
        this.total = res.content.total
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getListByPage()
    },
    handleDel(id) {
      this.$confirm('确认删除？').then(_ => {
        del(id).then(res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getListByPage()
        })
      })
    }
  }
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}

.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}

.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
